package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.card.CardAddReqDTO;
import com.yixin.hospital.entity.dto.req.card.CardEditReqDTO;
import com.yixin.hospital.entity.vo.CardVo;
import com.yixin.hospital.service.CardService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/card")
@Api(value = "卡管理", tags = "卡管理")
@Slf4j
public class CardController {

    @Resource
    private CardService cardService;

    @GetMapping("/list")
    @ApiOperation(value = "卡列表")
    public ApiResult<List<CardVo>> list() {
        return cardService.allList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加卡")
    public ApiResult<?> add(@Valid @RequestBody CardAddReqDTO cardAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return cardService.add(cardAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑卡")
    public ApiResult<?> edit(@Valid @RequestBody CardEditReqDTO dardEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return cardService.edit(dardEditReqDTO, userName);

    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除卡")
    public ApiResult<?> remove(@PathVariable int id) {
        cardService.removeById(id);
        return ApiResult.success();
    }

}
